import axios from 'axios';
import { getUrlParam } from "../util";
import qs from 'qs';

const token = getUrlParam().token || '';

const axiosIns = axios.create({
    baseURL: '/sx/api',
    headers: {'x-auth-token' : token}
});

axiosIns.interceptors.response.use(res => {
    if (res.data && +res.data.code === 200) {
        return res.data.data;
    } else {
        return Promise.reject(res.data);
    }
}, error => {
    return Promise.reject(error);
});

export default {
    get(url, params, config) {
        return axiosIns.get(url, {
            params,
            ...config
        });
    },
    post(url, data, config) {
        return axiosIns.post(url, qs.stringify(data), config);
    }
}
